package com.bwie.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bwie.mapper.StudentMapper;
import com.bwie.pojo.*;
import com.bwie.service.ICityService;
import com.bwie.service.IClassService;
import com.bwie.service.ISchoolService;
import com.bwie.service.StudentService;
import com.bwie.util.R;
import com.bwie.vo.BindVo;
import com.bwie.vo.PageVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * @program: day0617mounth
 * @ClassName StudentServiceImpl
 * @description:
 * @author: 段阳
 * @create: 2024-06-17 10:22
 **/
@Service
@Transactional
public class StudentServiceImpl implements StudentService {

    @Autowired
    private StudentMapper studentMapper;

    @Autowired
    private ICityService iCityService;

    @Autowired
    private IClassService iClassService;

    @Autowired
    private ISchoolService iSchoolService;

    @Override
    public R findStuList(PageVo pageVo) {
        Page<TbStudent> tbStudentPage = new Page<>(pageVo.getPageNum(), pageVo.getPageSize());
        QueryWrapper<TbStudent> queryWrapper = new QueryWrapper<>();

        if (pageVo.getCid()!=null){
            queryWrapper.lambda().eq(TbStudent::getClassId,pageVo.getCid());
        }else {
            queryWrapper.lambda().ne(TbStudent::getClassId,0);
        }
        if (!StringUtils.isBlank(pageVo.getStuName())){
            queryWrapper.lambda().like(TbStudent::getStudentName,pageVo.getStuName());
        }
        if (!StringUtils.isBlank(pageVo.getStuNumber())){
            queryWrapper.lambda().like(TbStudent::getStudentNumber,pageVo.getStuNumber());
        }
        if (!StringUtils.isBlank(pageVo.getStuMobile())){
            queryWrapper.lambda().eq(TbStudent::getStudentMobile,pageVo.getStuMobile());
        }
        if (!StringUtils.isBlank(pageVo.getStartTime())){
            queryWrapper.lambda().ge(TbStudent::getCreateTime,pageVo.getStartTime());
        }
        if (!StringUtils.isBlank(pageVo.getEndTime())){
            queryWrapper.lambda().le(TbStudent::getCreateTime,pageVo.getEndTime());
        }

        Page<TbStudent> studentPage = studentMapper.selectPage(tbStudentPage, queryWrapper);
        List<TbStudent> students = studentPage.getRecords();
        for (TbStudent student : students) {
            Integer id = student.getClassId();
            if(id!=null && id !=0){
                R classById = iClassService.getClassById(id);
                String s = JSON.toJSONString(classById.getData());
                TbClass tbClass = JSON.parseObject(s, TbClass.class);
                if (tbClass!=null){
                    student.setClassName(tbClass.getClassName());
                }
            }
        }
        return R.success("",studentPage);
    }

    @Override
    public R addStuInfo(TbStudent tbStudent) {
        if (StringUtils.isBlank(tbStudent.getStudentName())){
            return R.error(500,"学生姓名不能为空！");
        }
        if (StringUtils.isBlank(tbStudent.getStudentMobile())){
            return R.error(500,"学生电话不能为空！");
        }
        if (StringUtils.isBlank(tbStudent.getIdCard())){
            return R.error(500,"学生身份证不能为空！");
        }
        if (StringUtils.isBlank(tbStudent.getAddress())){
            return R.error(500,"学生地址不能为空！");
        }
        if (StringUtils.isBlank(tbStudent.getFamilyName())){
            return R.error(500,"父母姓名不能为空！");
        }
        if (StringUtils.isBlank(tbStudent.getFamilyMobile())){
            return R.error(500,"父母电话不能为空！");
        }

        tbStudent.setStudentNumber(UUID.randomUUID().toString());
        tbStudent.setClassId(0);
        tbStudent.setCreateTime(new Date());
        int insert = studentMapper.insert(tbStudent);
        if(insert>0){
            return R.success("添加成功");
        }else {
            return R.error(500,"添加失败");
        }

    }

    @Override
    public R findClassList(Integer id) {

            R classList = iClassService.findClassList(id);
            String s = JSON.toJSONString(classList.getData());
            List<TbClass> tbClasses = JSON.parseArray(s, TbClass.class);
        for (TbClass tbClass : tbClasses) {
            Integer sid = tbClass.getCollegeId();
            R collegeBySid = iSchoolService.findCollegeBySid(sid);
            String s1 = JSON.toJSONString(collegeBySid.getData());
            TbCollege tbCollege = JSON.parseObject(s1, TbCollege.class);
            Integer cid = tbCollege.getSchoolId();
            R schoolById = iSchoolService.findSchoolById(cid);
            String s2 = JSON.toJSONString(schoolById.getData());
            TbSchool tbSchool = JSON.parseObject(s2, TbSchool.class);
            tbClass.setClassName(tbClass.getClassName()+"("+tbSchool.getSchoolName()+"-"+tbCollege.getCollegeName()+")");
        }
            return R.success("",tbClasses);



    }

    @Override
    public R bindStuToClass(BindVo bindVo) {
        if(bindVo.getCid()== null){
            return R.error(500,"班级必选！");
        }
        List<Integer> sids = bindVo.getSids();
        for (Integer sid : sids) {
            TbStudent tbStudent = studentMapper.selectById(sid);
            if (tbStudent.getClassId() == 0){
                tbStudent.setClassId(bindVo.getCid());
                studentMapper.updateById(tbStudent);
            }
        }
        System.out.println("11111122222222222211111");
        System.out.println("2222222222222222222222222");
        System.out.println("3333333333333333333333333");
        System.out.println("4444444444444444444444444");
        System.out.println("5555555555555555555555555");
        System.out.println("6666666666666666666666666");
        return R.success("绑定成功！");
    }

    @Override
    public R findCityList(Integer pid) {
        if(pid !=null){
            R cityList = iCityService.findCityList(pid);
            String s = JSON.toJSONString(cityList.getData());
            List<TbCity> tbCities = JSON.parseArray(s, TbCity.class);
            return R.success("",tbCities);
        }
        return R.error(500,"");
    }

    @Override
    public R findSchoolList() {
        R schoolList = iSchoolService.findSchoolList();
        String s = JSON.toJSONString(schoolList.getData());
        List<TbSchool> tbSchools = JSON.parseArray(s, TbSchool.class);
        return R.success("",tbSchools);
    }

    @Override
    public R findCollegeList(Integer sid) {
        R collegeList = iSchoolService.findCollegeList(sid);
        String s = JSON.toJSONString(collegeList.getData());
        List<TbCollege> tbColleges = JSON.parseArray(s, TbCollege.class);
        return R.success("",tbColleges);
    }

    @Override
    public R getIdCardInfo(String idCard) {
        TbStudent tbStudent = studentMapper.selectOne(new QueryWrapper<TbStudent>().lambda().eq(TbStudent::getIdCard, idCard));
        if (tbStudent!=null){
            return R.error(500,"");
        }
        return R.success("");
    }

    @Override
    public R getMobileInfo(String mobile) {
        TbStudent tbStudent = studentMapper.selectOne(new QueryWrapper<TbStudent>().lambda().eq(TbStudent::getStudentMobile, mobile));
        if (tbStudent!=null){
            return R.error(500,"");
        }
        return R.success("");
    }
}